package me.shurufa.holder;

import android.content.Context;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.RelativeSizeSpan;
import android.view.View;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import de.greenrobot.event.EventBus;
import me.shurufa.R;
import me.shurufa.model.Digest;
import me.shurufa.model.DigestListResponse;
import me.shurufa.model.FavouriteRetResponse;
import me.shurufa.net.NetworkConstants;
import me.shurufa.net.RequestServerTask;
import me.shurufa.utils.Global;
import me.shurufa.utils.HttpUtil;
import me.shurufa.utils.Utils;
import me.shurufa.widget.CircleImageView;
import me.shurufa.widget.likebutton.LikeButton;
import me.shurufa.widget.likebutton.OnLikeListener;

/**
 * RecyclerViewpager 书摘 笔记ViewHolder
 *
 * @FileName: me.shurufa.holder.MarkViewHolder.java
 * @author: kHRYSTAL
 * @email: 723526676@qq.com
 * @date: 2016-01-22 15:38
 */
public class MarkViewHolder extends BaseViewHolder implements OnLikeListener {

    @Bind(R.id.mark_from)
    TextView markFrom;
    @Bind(R.id.mark_avatar)
    CircleImageView markAvatar;
    @Bind(R.id.mark_digest)
    TextView markDigest;
    @Bind(R.id.mark_note)
    TextView markNote;
    @Bind(R.id.star_button)
    LikeButton starButton;

    private Context mContext;
    private Digest mDigest;
//mark from
    String creatorName;
    String date;
    String digestFrom;
    String pageNumberFrom;
    String partName;

    public MarkViewHolder(View itemView, Object parent) {
        super(itemView, parent);
        mContext = itemView.getContext();
        ButterKnife.bind(this, itemView);
        starButton.setOnLikeListener(this);
    }

    public void bind(Digest digest){
        if (digest==null)
            return;
        mDigest = digest;
        if (!TextUtils.isEmpty(digest.content))
            markDigest.setText(digest.content);
        else
            markDigest.setVisibility(View.GONE);
        if (digest.user_info!=null) {
            if (!TextUtils.isEmpty(digest.user_info.avatar))
                Glide.with(mContext).load(digest.user_info.avatar).into(markAvatar);
            else
                markAvatar.setImageResource(R.drawable.default_avatar);
            if (Global.getCurrentUid()!= digest.user_info.uid&&digest.favorite_info!=null){
                starButton.setVisibility(View.VISIBLE);
                starButton.setLiked(digest.favorite_info.my_favorite);
            }else
                starButton.setVisibility(View.GONE);
        }

        setMarkFrom(digest);

        if (!TextUtils.isEmpty(digest.note))
            markNote.setText(digest.note);
    }

    private void setMarkFrom(Digest digest){
        if (digest.user_info!=null)
            creatorName = digest.user_info.nickname;
        else creatorName="";

        if (!TextUtils.isEmpty(digest.created_at))
            date = " "+Utils.tranformDate(digest.created_at)+" ";
        else  date=" ";

        if (digest.menu_info!=null)
            partName = "\n"+String.format(mContext.getString(R.string.name_of_part),digest.menu_info.title);
        else partName = "\n"+mContext.getString(R.string.name_of_part);

        if (digest.book_info!=null)
            digestFrom = "\n" + String.format(mContext.getString(R.string.mark_from),digest.book_info.title);
        else digestFrom = "";

        if (digest.page_num!=0)
            pageNumberFrom = String.format(mContext.getString(R.string.page_of_book),String.valueOf(digest.page_num));
        else pageNumberFrom = "";

        SpannableString span = new SpannableString(partName);
        span.setSpan(new RelativeSizeSpan(0.7f), 0, span.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);

        markFrom.setText(TextUtils.concat(creatorName,date,digestFrom,pageNumberFrom,span));

    }

    @Override
    public void liked(LikeButton likeButton) {
        new RequestServerTask<FavouriteRetResponse>(FavouriteRetResponse.class) {
            @Override
            protected String requestServer() {
                Map<String, Object> params = new HashMap<String, Object>();
                params.put("excerptId",mDigest.id);
                params.put("token", Global.currentUser.token);
                return HttpUtil.post(NetworkConstants.FAVOURITE_DIGEST, params);
            }

            @Override
            protected boolean onFailure(FavouriteRetResponse result) {
                starButton.setLiked(mDigest.favorite_info.my_favorite);
                return super.onFailure(result);
            }

            @Override
            protected void onSuccess(FavouriteRetResponse result) {
                mDigest.favorite_info.my_favorite = true;
            }
        }.start();

    }

    @Override
    public void unLiked(LikeButton likeButton) {
        new RequestServerTask<FavouriteRetResponse>(FavouriteRetResponse.class) {
            @Override
            protected String requestServer() {
                Map<String, Object> params = new HashMap<String, Object>();
                params.put("excerptId",mDigest.id);
                params.put("token", Global.currentUser.token);
                return HttpUtil.post(NetworkConstants.UNFAVOURITE_DIGEST, params);
            }

            @Override
            protected boolean onFailure(FavouriteRetResponse result) {
                starButton.setLiked(mDigest.favorite_info.my_favorite);
                return super.onFailure(result);
            }

            @Override
            protected void onSuccess(FavouriteRetResponse result) {
                mDigest.favorite_info.my_favorite = false;
            }
        }.start();
    }
}
